package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjPubUpMaterial;
import com.koron.css2.newProjectCenter.mapper.PjPubUpMaterialMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 苏小龙
 * @CreateTime: 2024-12-11
 * @Description: 材料信息多个保存
 * @Version: 1.0
 */
public class PjPubUpMaterialMoreSave implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            String rr = JsonUtils.objectToJson(req.getData());
            Map<String, Object> initDatas = JSON.parseObject(rr);
            String data = JSONObject.toJSONString(initDatas.get("list"));
            List<PjPubUpMaterial> list = JSON.parseObject(data, ArrayList.class);
            List<PjPubUpMaterial> converList = BeanUtil.copyToList(list, PjPubUpMaterial.class);
            PjPubUpMaterialMapper pjPubUpInvoiceMapper = factory.getMapper(PjPubUpMaterialMapper.class);
            if (!list.isEmpty()) {
                for (PjPubUpMaterial pubUpInvoice : converList) {
                    // 1. id
                    pubUpInvoice.setId(Tools.getObjectId());
                    // 1. updateTime,updateAccount,updateName
                    pubUpInvoice.setCreateInfo(userInfo);
                    // 2. 新增数据
                    // 2.1 判断typeId是否为空
                    if (StringUtils.isEmpty(pubUpInvoice.getTypeId()))
                        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请选择材料分类", null);
                    pjPubUpInvoiceMapper.insert(pubUpInvoice);
                }
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
        } catch (Exception e) {
            factory.close(false);
            logger.error("材料信息" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
        }
    }
}
